#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int M = 1e9 + 7;

int t, n, l[N], r[N];
ll dp[N][2];

struct Edge
{
    int to, next;
} e[N << 1]; // re了一发，因为边数是2倍的

int head[N], idx = 1;

void add_edge(int u, int v)
{
    e[idx].to = v;
    e[idx].next = head[u];
    head[u] = idx++;
}

void clear_graph()
{
    memset(head, -1, sizeof(head));
    memset(e, 0, sizeof(e));
    idx = 1;
}

ll inv(ll a, ll p = M)
{
    return (a == 1) ? 1 : inv(p % a, p) * (p - p / a) % p;
}

// a/b % p
ll frac_inv(ll a, ll b, ll p = M)
{
    return ((a % p) * inv(b, p)) % p;
}

void dfs(int u, int f = 0)
{
    for (int i = head[u]; ~i; i = e[i].next)
    {
        int v = e[i].to;
        if (v != f)
        {
            dfs(v, u);
            dp[u][0] += max(dp[v][0] + abs(l[u] - l[v]), dp[v][1] + abs(l[u] - r[v]));
            dp[u][1] += max(dp[v][0] + abs(r[u] - l[v]), dp[v][1] + abs(r[u] - r[v]));
        }
    }
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0); // tle一发，因为没有读入加速
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> l[i] >> r[i];
        }
        clear_graph();
        memset(dp, 0, sizeof(dp));

        for (int i = 0; i < n - 1; i++)
        {
            int u, v;
            cin >> u >> v;
            add_edge(u, v), add_edge(v, u);
        }

        dfs(1);

        cout << max(dp[1][0], dp[1][1]) << endl;
    }

    return 0;
}